#include "GameControl.h"





GameControl::GameControl()
{
}


GameControl::~GameControl()
{
}

bool GameControl::initMove()
{
	Ogre::ConfigFile cf; 
	cf.load("settings.cfg");
	Ogre::String emu_enabled = cf.getSetting("enabled","Emulator");

	if (emu_enabled.compare("true")==0)
	{
		move = Move::createEmulator();						
	}
	else
		move = Move::createRealDevice();


	if (move->initMoves()<1)
	{
		return false;
	}
	move->initCamera();

	return true;
}

void GameControl::copyCameraImageToTexture(Ogre::TexturePtr texture)
{
	PBYTE eyeBuffer=move->getEyeBuffer();
	if (eyeBuffer)
	{
		Ogre::HardwarePixelBufferSharedPtr buffer = texture->getBuffer(0,0);
		buffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
		const Ogre::PixelBox &pb = buffer->getCurrentLock();
		PBYTE data = static_cast<PBYTE>(pb.data);
		memcpy(data,eyeBuffer,320*240*4);

		for (size_t j = 0; j < 320; j++)
			for(size_t i = 0; i < 240; i++)
			{
				*data++; // B
				*data++; // G
				*data++; // R
				*data++ = 255; // A
			}
		buffer->unlock();
	}
}

Ogre::Quaternion GameControl::getMoveOrientation(int num)
{
	Ogre::Quaternion q = move->getOrientation(num);
	//std::ofstream file;									//for creating log file
	//file.open("test.emu", std::ofstream::app);
	//file<<std::setiosflags(std::ios_base::showpos| std::ios_base::fixed)<<q.w
	//	<<std::setiosflags(std::ios_base::showpos| std::ios_base::fixed)<<";"<<q.x
	//	<<std::setiosflags(std::ios_base::showpos| std::ios_base::fixed)<<";"<<q.y
	//	<<std::setiosflags(std::ios_base::showpos| std::ios_base::fixed)<<";"<<q.z<<std::endl;
	return q;
}

Ogre::Vector3 GameControl::getMovePosition(int id)
{
	Ogre::Vector3 v = move->getPosition(id);
	return v;
}

void GameControl::PassKeyPressed(const OIS::KeyEvent &arg)
{
	move->PassKeyPressed(arg);
}

void GameControl::PassKeyReleased(const OIS::KeyEvent &arg)
{
	move->PassKeyReleased(arg);
}

void GameControl::PassmouseMoved( const OIS::MouseEvent &arg )
{
	move->PassmouseMoved(arg);
}

void GameControl::PassWindowParam( int width, int height)
{
	move->PassWindowParam(width,height);
}